package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.ProductionInfo;
import me.zhengjie.repository.ProductionInfoRepository;
import me.zhengjie.service.ProductionInfoService;
import me.zhengjie.service.dto.ProductionInfoDto;
import me.zhengjie.service.dto.ProductionInfoQueryCriteria;
import me.zhengjie.service.mapper.ProductionInfoMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

@Service
@RequiredArgsConstructor
public class ProductionInfoServiceImpl implements ProductionInfoService {

    private final ProductionInfoRepository productionInfoRepository;
    private final ProductionInfoMapper productionInfoMapper;

    @Override
    public PageResult<ProductionInfoDto> queryAll(ProductionInfoQueryCriteria criteria, Pageable pageable){
        Page<ProductionInfo> page = productionInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(productionInfoMapper::toDto));
    }

    @Override
    public List<ProductionInfoDto> queryAll(ProductionInfoQueryCriteria criteria){
        return productionInfoMapper.toDto(productionInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ProductionInfoDto findById(Integer id) {
        ProductionInfo productionInfo = productionInfoRepository.findById(id).orElseGet(ProductionInfo::new);
        ValidationUtil.isNull(productionInfo.getId(),"ProductionInfo","id",id);
        return productionInfoMapper.toDto(productionInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ProductionInfo resources) {
        productionInfoRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProductionInfo resources) {
        ProductionInfo productionInfo = productionInfoRepository.findById(resources.getId()).orElseGet(ProductionInfo::new);
        ValidationUtil.isNull( productionInfo.getId(),"ProductionInfo","id",resources.getId());
        productionInfo.copy(resources);
        productionInfoRepository.save(productionInfo);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            productionInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProductionInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProductionInfoDto productionInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("生产类型", productionInfo.getProductionType());
            map.put("容器编码", productionInfo.getPalletCode());
            map.put("工位编码", productionInfo.getStationCode());
            map.put("叠合人员", productionInfo.getLayUpBy());
            map.put("拆解人员", productionInfo.getUnbindingBy());
            map.put("工程号", productionInfo.getWorkNum());
            map.put("外层板数量", productionInfo.getQty());
            map.put("材质", productionInfo.getMaterial());
            map.put("材料夹法", productionInfo.getLayUpName());
            map.put("叠合开始时间", productionInfo.getLayUpStartTime());
            map.put("叠合完成时间", productionInfo.getLayUpFinishTime());
            map.put("拆解开始时间", productionInfo.getUnbindingStartTime());
            map.put("拆解结束时间", productionInfo.getUnbindingFinishTime());
            map.put("压合开始时间", productionInfo.getPressingStartTime());
            map.put("压合结束时间", productionInfo.getPressingFinishTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
